#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include <time.h>

#include "h/starfolk.h"

#define LEN 81
#define MON_LEN 15
#define MON_ABB_LEN 4

struct Month {
    char name[MON_LEN];
    char abbr[MON_ABB_LEN];
    int days;
};


struct Month Mon[12] = {
    {
        "january", "jan", 31
    }, {
        "february", "feb", 28
    }, {
        "march", "mar", 31
    }, {
        "april", "apr", 30
    }, {
        "may", "may", 31
    }, {
        "june", "jun", 30
    }, {
        "july", "jul", 31
    }, {
        "august", "aug", 31
    }, {
        "september", "sep", 30
    }, {
        "october", "oct", 31
    }, {
        "november", "nov", 30
    }, {
        "december", "dec", 31
    }
};

void print_bem(const struct bem *);

char * to_lower(char *);

int get_y_days(int d, int m, int y, struct tm *);

int main(void)
{
   int i;

   time_t date;
   int month;
   int year;
   int day;

   char smonth[20];
   char * ept;
   int monlen;

   int flag;

   time(&date);

   struct tm * loc = localtime(&date);
   struct tm res_tm;

   year = loc->tm_year + 1900;
   month = loc->tm_mon + 1;
   day = loc->tm_mday;

   puts("Please enter date (format: [day] [month] [year])");
   puts("(enter 0 to quit).");


   while ((flag = scanf("%d %12s %d", &day, smonth, &year)) && day != 0) {

       if (flag > 1) {
            month = strtol(smonth, &ept, 10);
            monlen = strlen(smonth);

            if (ept != smonth) {
                if (month <=0 || month >12) {
                    puts("month should greater than 0 and less than 13");
                    continue;
                }

            } else if (monlen > 2) {     //这里可以直接使用ept > 0来判断
                to_lower(smonth);

                for (i=0; i<12; ++i) {
                    if (strncmp(Mon[i].name, smonth, monlen) == 0)
                        break;
                }
                if (i == 12) {
                    puts("month name is invalid value.");
                    continue;
                }
                month = i+1;

            } else {
                puts("month name is invalid value.");
                continue;
            }
        }

       if (flag > 2) {
           if (year < 1900) {
               puts("It can only handle years after 1900.");
               continue;
           }
       }

       flag = get_y_days(day, month, year, &res_tm);
       if (-1 == flag)
           puts("get year's num of days failed!");
       else
           printf("From the beginning of the year to the ");
           printf("%d/%d/%d days is: %d\n", res_tm.tm_mday, res_tm.tm_mon + 1, res_tm.tm_year + 1900, flag);

       puts("Please enter a date.");

   }

}

char * to_lower(char * str)
{
    int i=0;
    while ((*(str+i) = tolower(*(str+i))))
        ++i;

    return str;
}

int get_y_days(int day, int month, int year, struct tm * tms)
{
    tms->tm_year = year - 1900;
    tms->tm_mon = month - 1;
    tms->tm_mday = day;
    tms->tm_hour = 0;
    tms->tm_min = 0;
    tms->tm_sec = 0;
    tms->tm_isdst = -1;

    if (mktime(tms) == (time_t)-1)
        return -1;

    return tms->tm_yday;
}
